package interfaces.clients.dispatcher.commands;

import java.io.IOException;
import java.io.ObjectInputStream;

/**
 * @author Mihail A. Muhin
 */
public class CommandReader {
	public static BaseCommand read(ObjectInputStream ois) throws ReadCorrectableException,
			NoClassCorrectableDebugException {
		BaseCommand res = null;
		String class_name = null;
		try {
			class_name = (String) (ois.readObject());
			res = (BaseCommand) (
					Class.forName(
							CommandReader.class.getPackage().getName().concat(".").concat(class_name)
					).newInstance()
			);
			res.read(ois);
		} catch (IllegalAccessException e) {
			throw new NoClassCorrectableDebugException("Wrong class - ".concat(class_name), e);
		} catch (IOException e) {
			throw new ReadCorrectableException("Couldn't read command from stream");
		} catch (InstantiationException e) {
			throw new NoClassCorrectableDebugException("Wrong class - ".concat(class_name), e);
		} catch (ClassNotFoundException e) {
			throw new ReadCorrectableException("Command class not found");
		}
		return res;
	}

	private CommandReader() {

	}
}
